package com.qys.livesMall.financial.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.financial.model.dto.sys.FinancialOfflinePageDTO;
import com.qys.livesMall.financial.model.dto.sys.FinancialOfflineSaveDTO;
import com.qys.livesMall.financial.model.dto.sys.FinancialOfflineUserDetailPageDTO;
import com.qys.livesMall.financial.model.dto.sys.FinancialOfflineUserPageDTO;
import com.qys.livesMall.financial.model.vo.sys.FinancialOfflinePageVO;
import com.qys.livesMall.financial.model.vo.sys.FinancialOfflineUserDetailVO;
import com.qys.livesMall.financial.model.vo.sys.FinancialOfflineUserVO;
import com.qys.livesMall.financial.model.vo.sys.FinancialOfflineVO;
import com.qys.livesMall.financial.service.FinancialOfflineService;
import com.qys.livesMall.financial.service.FinancialOfflineUserDetailService;
import com.qys.livesMall.financial.service.FinancialOfflineUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 线下理财 前端控制器
 *
 * @author
 * @since 2023-06-10
 */
@RestController
@Tag(name = "后台管理-线下理财")
@RequestMapping("/sys/financial/offline")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysFinancialOfflineController {

    private final FinancialOfflineService financialOfflineService;

    private final FinancialOfflineUserService financialOfflineUserService;

    private final FinancialOfflineUserDetailService financialOfflineUserDetailService;

    @PostMapping("/page")
    @Operation(summary = "分页查询线下理财信息")
    public R<Page<FinancialOfflinePageVO>> page(@RequestBody FinancialOfflinePageDTO pageDTO) {
        return R.ok(financialOfflineService.pageBySys(pageDTO));
    }

    @PostMapping("/add")
    @Operation(summary = "新增线下理财信息")
    public R<Void> add(@RequestBody @Validated FinancialOfflineSaveDTO saveDTO) {
        financialOfflineService.save(saveDTO);
        return R.ok();
    }

    @PutMapping("/edit")
    @Operation(summary = "编辑线下理财信息")
    public R<Void> edit(@RequestBody @Validated FinancialOfflineSaveDTO saveDTO) {
        financialOfflineService.save(saveDTO);
        return R.ok();
    }

    @Operation(
            summary = "删除线下理财",
            parameters = {
                    @Parameter(name = "id", description = "理财id")
            }
    )
    @DeleteMapping(value = "/delete/{id}")
    public R<Void> deleteById(@PathVariable Long id) {
        financialOfflineService.deleteById(id);
        return R.ok();
    }

    @PutMapping("/shelves/{id}")
    @Operation(
            summary = "上架下架",
            parameters = {
                    @Parameter(name = "id", description = "理财id")
            }
    )
    public R<Void> shelves(@PathVariable("id") Long id) {
        financialOfflineService.shelves(id);
        return R.ok();
    }

    @GetMapping("/{id}")
    @Operation(
            summary = "根据id查询任务信息",
            parameters = {
                    @Parameter(name = "id", description = "理财id")
            }
    )
    public R<FinancialOfflineVO> queryById(@PathVariable Long id) {
        return R.ok(financialOfflineService.queryById(id));
    }

    @PostMapping("/user/page")
    @Operation(summary = "分页查询线下理财用户参与信息")
    public R<IPage<FinancialOfflineUserVO>> userPage(@RequestBody FinancialOfflineUserPageDTO pageDTO) {
        return R.ok(financialOfflineUserService.userPage(pageDTO));
    }

    @PostMapping("/userdetail/page")
    @Operation(summary = "分页查询线下理财收益记录")
    public R<Page<FinancialOfflineUserDetailVO>> userdetailpage(@RequestBody FinancialOfflineUserDetailPageDTO pageDTO) {
        return R.ok(financialOfflineUserDetailService.userdetailpage(pageDTO));
    }
}
